package com.caiwangdai.app.widget

import android.content.Context
import android.graphics.*
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.View
import com.caiwangdai.app.R

/**
 * @project Cashloan_Android
 * @author 艹羊
 * @date 2017/10/25 11:38
 * @description
 */
class CameraBorder : View {
    var borderColor: Int = Color.BLUE//颜色
    var borderThick: Float = dip2px(4f)//厚度
    var borderStretch: Float = dip2px(30f)//伸展度

    constructor(context: Context) : super(context, null, 0)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)

    var borderP: Paint? = null
    var fullRect: RectF = RectF()

    var borderLeftTop: Path = Path()
    var borderLeftBottom: Path = Path()

    var borderRightTop: Path = Path()
    var borderRightBottom: Path = Path()

    init {
        borderColor = ContextCompat.getColor(context, R.color.colorPrimary)


        borderP = Paint()
        borderP?.strokeWidth = 10f
        borderP?.color = borderColor
        borderP?.style = Paint.Style.FILL
        borderP?.isAntiAlias = true
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        fullRect.left = 0f
        fullRect.top = 0f
        fullRect.right = w.toFloat()
        fullRect.bottom = w.toFloat()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val left = fullRect.left
        val top = fullRect.top
        val right = fullRect.right
        val bottom = fullRect.bottom

        //left-top
        borderLeftTop.reset()
        borderLeftTop.moveTo(left, top)
        borderLeftTop.lineTo(borderStretch, top)
        borderLeftTop.lineTo(borderStretch, borderThick)
        borderLeftTop.lineTo(borderThick, borderThick)
        borderLeftTop.lineTo(borderThick, borderStretch)
        borderLeftTop.lineTo(left, borderStretch)
        borderLeftTop.close()
        canvas?.drawPath(borderLeftTop, borderP)

        //left_bottom
        borderLeftBottom.reset()
        borderLeftBottom.moveTo(left, bottom)
        borderLeftBottom.lineTo(left + borderStretch, bottom)
        borderLeftBottom.lineTo(left + borderStretch, bottom - borderThick)
        borderLeftBottom.lineTo(left + borderThick, bottom - borderThick)
        borderLeftBottom.lineTo(left + borderThick, bottom - borderStretch)
        borderLeftBottom.lineTo(left, bottom - borderStretch)
        borderLeftBottom.close()
        canvas?.drawPath(borderLeftBottom, borderP)

        //right_top
        borderRightTop.reset()
        borderRightTop.moveTo(right, top)
        borderRightTop.lineTo(right, top + borderStretch)
        borderRightTop.lineTo(right - borderThick, top + borderStretch)
        borderRightTop.lineTo(right - borderThick, top + borderThick)
        borderRightTop.lineTo(right - borderStretch, top + borderThick)
        borderRightTop.lineTo(right - borderStretch, top)
        borderRightTop.close()
        canvas?.drawPath(borderRightTop, borderP)

        //right_bottom
        borderRightBottom.reset()
        borderRightBottom.moveTo(right, bottom)
        borderRightBottom.lineTo(right, bottom - borderStretch)
        borderRightBottom.lineTo(right - borderThick, bottom - borderStretch)
        borderRightBottom.lineTo(right - borderThick, bottom - borderThick)
        borderRightBottom.lineTo(right - borderStretch, bottom - borderThick)
        borderRightBottom.lineTo(right - borderStretch, bottom)
        borderRightBottom.close()
        canvas?.drawPath(borderRightBottom, borderP)
    }

    //根据手机的分辨率从 dp 的单位 转成为 px(像素)
    fun dip2px(dpValue: Float): Float {
        val scale = context.resources.displayMetrics.density
        return dpValue * scale + 0.5f
    }
}